//
//  HEActiveDetailsViewController.m
//  HealthFamily
//
//  Created by 吴欧 on 2020/11/26.
//  Copyright © 2020 honglutec. All rights reserved.
//

#import "HEActiveDetailsViewController.h"
#import "LEEAlert.h"
#import "HECreateOrderResponse.h"
#import "HEPayPopView.h"
#import "HEQRPayView.h"
#import "HEWxPayReqModel.h"
#import "HEPayResultResViewController.h"

@interface HEActiveDetailsViewController ()<BMKMapViewDelegate, HEPayPopViewDelegate>

@property (nonatomic, strong) UIScrollView *scrollView;
// 约束参照视图,也是容器视图
@property (nonatomic, strong) UIView *contentView;

@property (nonatomic, strong) UILabel *titleLb;
@property (nonatomic, strong) UILabel *codeLb;

@property (nonatomic, strong) UILabel *acTimeLb;
@property (nonatomic, strong) UILabel *acPepoleLb;

@property (nonatomic, strong) UITextView *contentLb;
@property(nonatomic,weak) BMKMapView *mapView;//地图

@property (nonatomic, strong) HEActiveModel *model;

@property (nonatomic, strong)  UILabel * mainCntLb;
@property (nonatomic, strong)  UILabel * audioResLb;
@property (nonatomic, strong)  UILabel * remarkLb;
@property (nonatomic, strong)  UILabel *priceLb;

@property (nonatomic, strong) HEOrderModel * order;
@property (nonatomic, strong) NSArray <HEPaymentModel *>*paymentsList; // 支付方式
@property (nonatomic, strong) QMUIFillButton *agreeBtn;
@property (nonatomic, strong) UIView *activeWarp;
@property (nonatomic, strong) UIView *btnWarp;
@property (nonatomic, strong) QMUIFillButton *stateBtn;
@property (nonatomic, strong) QMUIFillButton *complBtn;

@property (nonatomic, strong) QMUITextView *textView;


@end

@implementation HEActiveDetailsViewController

-(void)viewWillAppear:(BOOL)animated {
    [_mapView viewWillAppear];
    _mapView.delegate = self; // 此处记得不用的时候需要置nil，否则影响内存的释放
}

-(void)viewWillDisappear:(BOOL)animated {
    [_mapView viewWillDisappear];
    _mapView.delegate = nil; // 不用时，置nil
}

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    // 布局子控件
    // 设置scrollView约束
    [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view).insets(UIEdgeInsetsMake(ZXNavBarHeight, 0, 0, 0));
    }];
    
    // 设置参照视图的约束
    [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.scrollView);
        make.width.equalTo(self.scrollView);
//        make.height.greaterThanOrEqualTo(@0.0f);
    }];
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:KNotificationPaySuccess object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:KNotificationPayFailure object:nil];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"活动详请";
    
    [self loadData];
    [self loadPaydata];
    [self initUI];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(paySuccess) name:KNotificationPaySuccess object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(payFialure) name:KNotificationPayFailure object:nil];
}

- (void)initUI {
    
    // 1. 添加scrollView
    [self.view addSubview:self.scrollView];
    // 2. 添加参照视图
    [self.scrollView addSubview:self.contentView];
    
    UIImageView *backView = [UIImageView new];
    backView.image = IMAGE_NAMED(@"activity_title_bg");
    [_contentView addSubview:backView];
     
    [backView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(_contentView);
    }];
    
    
    _titleLb = [UILabel new];
    _titleLb.text = @"-";
    _titleLb.font = DYBlodFont(17);
    _titleLb.textColor = KWhiteColor;
    _titleLb.numberOfLines = 0;
    _titleLb.textAlignment = NSTextAlignmentCenter;
    [_contentView addSubview: _titleLb];
    
    _codeLb = [UILabel new];
    _codeLb.text = @"";
    _codeLb.font = DYBlodFont(17);
    _codeLb.textColor = KWhiteColor;
    _codeLb.numberOfLines = 0;
    _codeLb.textAlignment = NSTextAlignmentCenter;
    [_contentView addSubview: _codeLb];
    
    _acTimeLb = [UILabel new];
    _acTimeLb.text = @"活动时间：-";
    _acTimeLb.font = DYBlodFont(17);
    _acTimeLb.textColor = KWhiteColor;
    _acTimeLb.textAlignment = NSTextAlignmentCenter;
    [_contentView addSubview: _acTimeLb];
    
    _acPepoleLb = [UILabel new];
    _acPepoleLb.text = @"参与人数：0";
    _acPepoleLb.font = DYBlodFont(17);
    _acPepoleLb.textColor = KWhiteColor;
    _acPepoleLb.textAlignment = NSTextAlignmentCenter;
    [_contentView addSubview: _acPepoleLb];
    
    [_titleLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_contentView).offset(30);
        make.left.equalTo(_contentView).offset(30);
        make.right.equalTo(_contentView).offset(-30);
    }];
    
    [_codeLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_titleLb.mas_bottom).offset(10);
        make.left.equalTo(_contentView).offset(30);
        make.right.equalTo(_contentView).offset(-30);
    }];
    
    [_acTimeLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_codeLb.mas_bottom).offset(10);
        make.left.right.equalTo(_contentView);
    }];
    
    [_acPepoleLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_acTimeLb.mas_bottom).offset(10);
        make.left.right.equalTo(_contentView);
    }];
    
    
    UIView *warp = [UIView new];
    warp.backgroundColor = KWhiteColor;
    [self.contentView addSubview:warp];
    _activeWarp = warp;
 
    warp.layer.shadowColor = [UIColor colorWithRed:13/255.0 green:98/255.0 blue:218/255.0 alpha:0.08].CGColor;
    warp.layer.shadowOffset = CGSizeMake(0,5);
    warp.layer.shadowOpacity = 1;
    warp.layer.shadowRadius = 6;
    warp.layer.cornerRadius = 10;
 
    [warp mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_acPepoleLb.mas_bottom).mas_offset(20);
        make.left.equalTo(self.contentView).mas_offset(15);
        make.right.equalTo(self.contentView).mas_offset(-15);
//        make.bottom.equalTo(self.contentView).mas_offset(-10);
    }];
    
    QMUIButton *qqView = [[QMUIButton alloc] init];
    qqView.imagePosition = QMUIButtonImagePositionLeft;// 将图片位置改为在文字下方
    qqView.tintColorAdjustsTitleAndImage = CNavBgColor;
    qqView.adjustsImageTintColorAutomatically = NO;
    qqView.spacingBetweenImageAndTitle = 10;
    qqView.userInteractionEnabled = NO;
    [qqView setImage:IMAGE_NAMED(@"activity_icon") forState:UIControlStateNormal];
    [qqView setTitle:@"活动详请" forState:UIControlStateNormal];
    qqView.titleLabel.font = DYBlodFont(18);
    [warp addSubview:qqView];
    
    [qqView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(warp).offset(15);
        make.centerX.equalTo(warp);
    }];
    
    
    // middle
    UILabel *mainCntLb = [UILabel new];
    mainCntLb.text = @"活动内容：";
    mainCntLb.font = DYBlodFont(16);
    mainCntLb.textColor = CNavLightBlackColor;
    [warp addSubview: mainCntLb];
    
    UILabel *audioResLb = [UILabel new];
    audioResLb.text = @"参与方式：";
    audioResLb.font = DYBlodFont(16);
    audioResLb.textColor = CNavLightBlackColor;
    [warp addSubview: audioResLb];
    
    UILabel *priceLb = [UILabel new];
    priceLb.text = @"报名费用：";
    priceLb.font = DYBlodFont(16);
    priceLb.textColor = CNavLightBlackColor;
    [warp addSubview: priceLb];
    
    UILabel *remarkTitlLb = [UILabel new];
    remarkTitlLb.text = @"活动地点：";
    remarkTitlLb.font = DYBlodFont(16);
    remarkTitlLb.textColor = CNavLightBlackColor;
    [warp addSubview: remarkTitlLb];
    
    UILabel *detailLb = [UILabel new];
    detailLb.text = @"详请描述：";
    detailLb.font = DYBlodFont(16);
    detailLb.textColor = CNavLightBlackColor;
    [warp addSubview: detailLb];
 
    _mainCntLb = [UILabel new];
    _mainCntLb.text = @"-";
    _mainCntLb.font = DYFont(16);
    _mainCntLb.textColor = CNavDargGrayColor;
    _mainCntLb.numberOfLines = 0;
    [warp addSubview:_mainCntLb];
    
    _audioResLb = [UILabel new];
    _audioResLb.text = @"-";
    _audioResLb.font = DYFont(16);
    _audioResLb.textColor = CNavDargGrayColor;
    _audioResLb.numberOfLines = 0;
    [warp addSubview:_audioResLb];
    
    _priceLb = [UILabel new];
    _priceLb.text = @"-";
    _priceLb.font = DYFont(16);
    _priceLb.textColor = CNavDargGrayColor;
    _priceLb.numberOfLines = 0;
    [warp addSubview:_priceLb];
    
    _remarkLb = [UILabel new];
    _remarkLb.text = @"-";
    _remarkLb.font = DYFont(16);
    _remarkLb.textColor = CNavDargGrayColor;
    _remarkLb.numberOfLines = 0;
    [warp addSubview:_remarkLb];

  
    [mainCntLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(qqView.mas_bottom).offset(10);
        make.left.equalTo(_titleLb.mas_left);
    }];
    [mainCntLb setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
 
    [audioResLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_mainCntLb.mas_bottom).offset(10);
        make.left.equalTo(_titleLb.mas_left);
    }];
    [audioResLb setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
    
    [priceLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_audioResLb.mas_bottom).offset(10);
        make.left.equalTo(_titleLb.mas_left);
    }];
    [priceLb setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
    
    [remarkTitlLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_priceLb.mas_bottom).offset(10);
        make.left.equalTo(_titleLb.mas_left);
    }];
    [remarkTitlLb setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
    

    [detailLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_remarkLb.mas_bottom).offset(10);
        make.left.equalTo(_titleLb.mas_left);
    }];
    [detailLb setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
    
    
    [_mainCntLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(qqView.mas_bottom).offset(10);
        make.left.equalTo(mainCntLb.mas_right);
        make.right.equalTo(warp).offset(-10);
    }];
    
    [_audioResLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_mainCntLb.mas_bottom).offset(10);
        make.left.equalTo(audioResLb.mas_right);
        make.right.equalTo(warp).offset(-10);
    }];
    
    [_priceLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_audioResLb.mas_bottom).offset(10);
        make.left.equalTo(priceLb.mas_right);
        make.right.equalTo(warp).offset(-10);
    }];
    
    [_remarkLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_priceLb.mas_bottom).offset(10);
        make.left.equalTo(remarkTitlLb.mas_right);
        make.right.equalTo(warp).offset(-10);
        make.bottom.equalTo(warp).offset(-10).priorityLow();
    }];
    
    
    
    // 描述
    _contentLb = [UITextView new];
    _contentLb.text = @"-";
    _contentLb.editable=NO;
    _contentLb.scrollEnabled = NO;
    _contentLb.textColor = CNavLightBlackColor;
    _contentLb.font = DYFont(15);
    _contentLb.backgroundColor = UIColor.clearColor;
    [warp addSubview:_contentLb];
    
    
    [_contentLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(detailLb.mas_bottom).mas_offset(10);
        make.left.equalTo(warp).mas_offset(15);
        make.right.equalTo(warp).mas_offset(-15);
    }];
    
    BMKMapView *mapView = [[BMKMapView alloc]init];
    [warp addSubview:mapView];
    [mapView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_contentLb.mas_bottom).offset(20);
        make.left.equalTo(warp).offset(10);
        make.right.equalTo(warp).offset(-10);
        make.height.mas_equalTo(@250);
        make.bottom.equalTo(warp).mas_offset(-20);
    }];
    self.mapView = mapView;
    //设置mapView的代理
    _mapView.delegate = self;
    [_mapView setRotateEnabled:NO];
    [_mapView setOverlookEnabled:NO];
    self.mapView.zoomLevel = 16;//地图显示的级别
 
    // 立即报名
    QMUIFillButton *agreeBtn = [[QMUIFillButton alloc] initWithFillColor:CNavBgColor titleTextColor:CNavBgFontColor];
    agreeBtn.titleLabel.font = UIFontMake(16);
    [agreeBtn setTitle:@"立即报名" forState:UIControlStateNormal];
    [self.view addSubview:agreeBtn];
    _agreeBtn = agreeBtn;

    [agreeBtn addTarget:self action:@selector(popPayViewClick) forControlEvents:UIControlEventTouchUpInside];

    [agreeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.width.mas_offset(110);
        make.height.mas_offset(40);
        make.bottom.equalTo(self.view).mas_offset(-30);
    }];
    
    [self bottomBtn];
}

- (void)bottomBtn {
    // 底部按钮
    UIView *btnWarp = [UIView new];
    _btnWarp = btnWarp;
    [self.contentView addSubview:btnWarp];

    [btnWarp mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_activeWarp.mas_bottom).mas_offset(30);
        make.right.equalTo(self.contentView);
        make.left.equalTo(self.contentView);
        make.height.mas_offset(36);
        make.bottom.equalTo(self.contentView).mas_offset(-(30 + ZXSafeAreaBottom));
    }];

    QMUIFillButton *stateBtn = [[QMUIFillButton alloc] initWithFillColor:CNavBgColor titleTextColor:CNavBgFontColor];
    stateBtn.titleLabel.font = UIFontMake(16);
    [stateBtn setTitle:@"已报名" forState:UIControlStateNormal];
    [btnWarp addSubview:stateBtn];
    _stateBtn = stateBtn;

    QMUIFillButton *complBtn = [[QMUIFillButton alloc] initWithFillColor:CPriceColor titleTextColor:CNavBgFontColor];
    complBtn.titleLabel.font = UIFontMake(16);
    [complBtn setTitle:@"投诉" forState:UIControlStateNormal];
    [btnWarp addSubview:complBtn];
    _complBtn = complBtn;

    [complBtn addTarget:self action:@selector(complBtnClick) forControlEvents:UIControlEventTouchUpInside];

    [stateBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(btnWarp).offset(-60);
        make.width.mas_offset(100);
        make.height.mas_offset(36);
    }];

    [complBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(btnWarp).offset(60);
        make.width.mas_offset(100);
        make.height.mas_offset(36);
    }];
}

- (void)popPayViewClick {
 
    [self crateOrder];
}

- (void)complBtnClick {
    UIView *warp = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
//    warp.backgroundColor = [UIColor colorWithRed:43/255.0f green:133/255.0f blue:208/255.0f alpha:1.0f];

    // 输入框
    _textView = [QMUITextView new];
    // 限制可输入的字符长度
    self.textView.maximumTextLength = 1500;
    self.textView.placeholder = @"请输入内容";
    self.textView.placeholderColor = UIColorPlaceholder; // 自定义 placeholder 的颜色
    self.textView.textContainerInset = UIEdgeInsetsMake(5, 7, 5, 7);
    self.textView.typingAttributes = @{NSFontAttributeName: UIFontMake(16),
                                       NSParagraphStyleAttributeName: [NSMutableParagraphStyle qmui_paragraphStyleWithLineHeight:20]
    };
    _textView.delegate = self;
    _textView.backgroundColor = CSplitLineColor;
    [warp addSubview:_textView];
    
    [_textView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(warp).mas_offset(10);
        make.left.equalTo(warp).mas_offset(0);
        make.right.equalTo(warp).mas_offset(-0);
        make.height.mas_greaterThanOrEqualTo(120);
    }];

 
    // 底部按钮
    UIView *btnWarp = [UIView new];
    [warp addSubview:btnWarp];

    [btnWarp mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_textView.mas_bottom).mas_offset(30);
        make.right.equalTo(warp);
        make.left.equalTo(warp);
        make.height.mas_offset(36);
    }];

    QMUIGhostButton *refuseBtn = [[QMUIGhostButton alloc] initWithGhostColor:CNavBgColor];
    refuseBtn.titleLabel.font = UIFontMake(16);
    [refuseBtn setTitle:@"取消" forState:UIControlStateNormal];
    [btnWarp addSubview:refuseBtn];

    QMUIFillButton *agreeBtn = [[QMUIFillButton alloc] initWithFillColor:CNavBgColor titleTextColor:CNavBgFontColor];
    agreeBtn.titleLabel.font = UIFontMake(16);
    [agreeBtn setTitle:@"确定" forState:UIControlStateNormal];
    [btnWarp addSubview:agreeBtn];

    [refuseBtn addTarget:self action:@selector(cancelClick) forControlEvents:UIControlEventTouchUpInside];
    [agreeBtn addTarget:self action:@selector(toSuggestClick) forControlEvents:UIControlEventTouchUpInside];

    [refuseBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(btnWarp);
        make.left.equalTo(btnWarp);
        make.width.mas_offset(100);
        make.height.mas_offset(36);
    }];

    [agreeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(btnWarp);
        make.right.equalTo(btnWarp);
        make.width.mas_offset(100);
        make.height.mas_offset(36);
    }];

    [LEEAlert alert].config
    .LeeTitle(@"投诉")
    .LeeAddCustomView(^(LEECustomView *custom) {
        custom.view = warp;
        custom.isAutoWidth = YES;
    })
    .LeeItemInsets(UIEdgeInsetsMake(10, 10, 10, 10)) // 想为哪一项设置间距范围 直接在其后面设置即可 ()
    .LeeItemInsets(UIEdgeInsetsMake(10, -10, 0, -10))
    .LeeShow();

    [_textView becomeFirstResponder];
}

- (void)cancelClick {
    [LEEAlert closeWithCompletionBlock:^{
        [self.textView resignFirstResponder];
    }];
}

- (void)toSuggestClick {
    if ([DYTools isBlankString:_textView.text]) {
        [Dialog toastBottom: @"请输入投诉内容"];
        return;
    }
    
    NSMutableDictionary *params = @{}.mutableCopy;
    [params setValue:_model.active_name forKey:@"support_title"];
    [params setValue:_textView.text forKey:@"support_txt"];
    [params setValue:[NSNumber numberWithInteger:_model.id] forKey:@"support_params"];
    [params setValue:@3 forKey:@"support_type"];
    [WOAFNetWorkingHandle POSTWithBaseUrl:KServerUrl suffixUrl:Ksupport parameters:params success:^(id  _Nonnull responseObject) {
        HEResponse *response = [HEResponse mj_objectWithKeyValues:responseObject];
        if (response.success) {
            [Dialog toastBottom:@"感谢您的反馈"];
            [LEEAlert closeWithCompletionBlock:^{
                [self.textView resignFirstResponder];
            }];
        }
    } failure:^(NSError * _Nonnull error) {
    }];
}

#pragma mark - load data
                      
- (void)loadPaydata {
  _paymentsList = @[].mutableCopy;
  [WOAFNetWorkingHandle GETWithBaseUrl:KServerUrl suffixUrl:Kpaymentlist parameters:@{@"appSource":@(KAppSource)} success:^(id  _Nonnull responseObject) {
      HEArrayResponse *response = [HEArrayResponse mj_objectWithKeyValues:responseObject];
      self.paymentsList = [HEPaymentModel mj_objectArrayWithKeyValuesArray:response.data];
  } failure:^(NSError * _Nonnull error) {
  }];
}

- (void)crateOrder {
    NSMutableDictionary *dict = @{}.mutableCopy;
    [dict setValue:[NSNumber numberWithInteger:self.model.id] forKey:@"id"];

    [WOAFNetWorkingHandle POSTWithBaseUrl:KServerUrl suffixUrl:KcreateActiveOrder parameters:dict success:^(id  _Nonnull responseObject) {
        HEResponse *response = [HEResponse mj_objectWithKeyValues:responseObject];
        if (response.success) {
            HECreateOrderResponse *data = [HECreateOrderResponse mj_objectWithKeyValues:response.data];
            self.order = data.order;
            
            HEPayPopView *vc = [[HEPayPopView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, kScreenHeight)];
            vc.detials = self.order;
            vc.paymentsList = _paymentsList;
            vc.delegate = self;
            [[UIApplication sharedApplication].keyWindow addSubview:vc];
            
        }
    } failure:^(NSError * _Nonnull error) {
        
    }];
}

- (void)loadData {
    [WOAFNetWorkingHandle POSTWithBaseUrl:KServerUrl suffixUrl:KActiveDetail parameters:@{@"id": [NSNumber numberWithInteger: _activeId]} success:^(id  _Nonnull responseObject) {
        NSLog(@"response：  %@", [responseObject mj_JSONString]);
        HEResponse *response = [HEResponse mj_objectWithKeyValues:responseObject];
        self.model = [HEActiveModel mj_objectWithKeyValues: response.data];
        [self refreshData];
    } failure:^(NSError * _Nonnull error) {
    }];
}

- (void)refreshData {
    
    _titleLb.text = _model.active_name;
    _acTimeLb.text = [NSString stringWithFormat:@"活动时间：%@", [NSDate timestampToDate:_model.start_date formaterMode:NSDateFormaterModeDefault]];
    _acPepoleLb.text = [NSString stringWithFormat:@"参与人数：%ld", _model.activity_reg_number];
    _contentLb.attributedText = _model.KDetailsHtml;
    
    _mainCntLb.text = nullToDef(_model.brief);
    _audioResLb.text = nullToDef(_model.active_secret);
    _remarkLb.text = nullToDef(_model.active_addr);
    _priceLb.text = [NSString stringWithFormat:@"%@元", [NSDecimalNumber rounding:_model.active_price afterPoint:2]];
    
    _btnWarp.hidden = YES;
    _agreeBtn.hidden = NO;
    if (_model.usSignUp == 1) {
        [_stateBtn setTitle:@"已报名" forState:UIControlStateNormal];
    }else if (_model.usSignUp == 2) {
        [_stateBtn setTitle:@"已使用" forState:UIControlStateNormal];
    }else if (_model.usSignUp == 3) {
        [_stateBtn setTitle:@"已退款" forState:UIControlStateNormal];
    }else {
        [_agreeBtn setTitle:@"立即报名" forState:UIControlStateNormal];
    }
    
    if (_model.usSignUp == 0) {
        _agreeBtn.enabled = YES;
        _codeLb.text = @"";
    }else {
        _btnWarp.hidden = NO;
        _agreeBtn.hidden = YES;
        _agreeBtn.enabled = NO;
        NSString *phone = [UserManager sharedUserManager].curUserInfo.username;
        _codeLb.text = [NSString stringWithFormat:@"核销串号：%@", [phone substringFromIndex: phone.length - 4]];
    }
    
//    BMKCoordinateRegion region ;//表示范围的结构体
//            region.center = CLLocationCoordinate2DMake(_model.latitude, _model.longitude);//中心点
//            region.span.latitudeDelta = 0.1;//经度范围（设置为0.1表示显示范围为0.2的纬度范围）
//            region.span.longitudeDelta = 0.1;//纬度范围
//            [_mapView setRegion:region animated:YES];
//
 
    [self createAnnotation];
    [_mapView setCenterCoordinate:CLLocationCoordinate2DMake(_model.latitude, _model.longitude) animated:YES];
    [_mapView zoomIn];
}

- (void)createAnnotation {
    [UIColor colorWithPatternImage:[UIImage imageNamed:@"21"]];
    
    //初始化一个点的注释
    BMKPointAnnotation * annotoation = [[BMKPointAnnotation alloc] init];
  
    //坐标
    annotoation.coordinate = CLLocationCoordinate2DMake(_model.latitude, _model.longitude);
  
    //将标注加在地图上
    [_mapView addAnnotation:annotoation];
}


#pragma mark - BMKMapViewDelegate
/**
 *根据anntation生成对应的View
 *@param mapView 地图View
 *@param annotation 指定的标注
 *@return 生成的标注View
 */
-(BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id<BMKAnnotation>)annotation{

      //如果是注释点
    if ([annotation isKindOfClass:[BMKPointAnnotation class]]) {
    
        //根据注释点,创建并初始化注释点视图
        BMKPinAnnotationView  *newAnnotation = [[BMKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"an"];
        newAnnotation.image = IMAGE_NAMED(@"gpers_icon");
        newAnnotation.animatesDrop = YES;
        return newAnnotation;
    }
    return nil;
}

#pragma mark - HEPayPopViewDelegate
#pragma mark - 待支付弹窗
- (void)popView:(id)payView payType:(NSString *)type {
    
    [HEPayManager sharedManager].order = self.order;

    // 发起支付
    if ([type isEqualToString:KWxPayCode]) {
        [self createWxPayRequest:self.order.id];
    }else if ([type isEqualToString:KAliPayCode]) {
        [self createAliPayRequest:self.order.id];
    }else if ([type isEqualToString:KQRPayCode]) {
        [self showQRPay];
    }
}


#pragma mark 二维码支付
- (void)showQRPay {
    NSString *token = [[UserManager sharedUserManager] getToken];
    KWeakSelf
    [HEQRPayView showWithURL: [NSString stringWithFormat:@"https://www.honglutec.com/oldPay/codePage.html?appSource=%d&orderId=%ld&payPlatform=%@&token=%@&providerPay=%d", KAppSource, self.order.id, KPayPlatform, token, _order.storeId == 1 ? 0 : 1] cancelBlock:^{
        [weakSelf payFialure];
    }];
}

#pragma mark 请求支付宝支付串
- (void)createAliPayRequest:(NSInteger)orderId {
    NSMutableDictionary *dict = @{}.mutableCopy;
    [dict setValue:[NSNumber numberWithInteger:orderId] forKey: @"orderId"];
    [dict setValue:@(KAppSource) forKey: @"appSource"];
    [dict setValue:KPayPlatform forKey: @"payPlatform"];
    NSString *url = _order.storeId == 1 ? KAliAppPay : KAliAppProPay;
    [WOAFNetWorkingHandle POSTWithBaseUrlByKeyValue:KServerUrl suffixUrl:url parameters:dict success:^(id  _Nonnull responseObject) {
        NSLog(@"response：  %@", [responseObject mj_JSONString]);
        HEObjectDataResponse *response = [HEObjectDataResponse mj_objectWithKeyValues:responseObject];
        if (response.success) {
            [[HEPayManager sharedManager] senAliPayRequest:response.data];
        }
    } failure:^(NSError * _Nonnull error) {
        
    }];
}
#pragma mark 请求微信支付串
- (void)createWxPayRequest:(NSInteger)orderId {
    NSMutableDictionary *dict = @{}.mutableCopy;
    [dict setValue:[NSNumber numberWithInteger:orderId] forKey: @"orderId"];
    [dict setValue:@(KAppSource) forKey: @"appSource"];
    [dict setValue:KPayPlatform forKey: @"payPlatform"];
    NSString *url = _order.storeId == 1 ? KWxAppPay : KWxAppProPay;
    [WOAFNetWorkingHandle POSTWithBaseUrlByKeyValue:KServerUrl suffixUrl:url parameters: dict success:^(id  _Nonnull responseObject) {
        NSLog(@"response：  %@", [responseObject mj_JSONString]);
        HEObjectDataResponse *response = [HEObjectDataResponse mj_objectWithKeyValues:responseObject];
        HEWxPayReqModel *wxModel = [HEWxPayReqModel mj_objectWithKeyValues:[DYTools getContainersObjectFromJsonString:response.data]];
            
        PayReq* req = [[PayReq alloc] init];
        req.partnerId = wxModel.partnerid;
        req.prepayId = wxModel.prepayid;
        req.nonceStr = wxModel.noncestr;
        req.timeStamp = wxModel.timestamp.unsignedIntValue;
        req.package = wxModel.package;
        req.sign = wxModel.sign;
        [WXApi sendReq:req completion:^(BOOL success) {
            if (!success) {
                [Dialog toastBottom:@"支付失败~"];
                [self payFialure];
            }
        }];

    } failure:^(NSError * _Nonnull error) {
        
    }];
}

#pragma mark - 支付回调
- (void)paySuccess {
    // 支付后跳转回来，请求失败的情况
    _model.usSignUp = 1;
    _btnWarp.hidden = NO;
    _agreeBtn.hidden = YES;
    _acPepoleLb.text = [NSString stringWithFormat:@"参与人数：%ld", (_model.activity_reg_number+1)];
    [_agreeBtn setTitle:@"已报名" forState:UIControlStateNormal];
    _agreeBtn.enabled = NO;
    NSString *phone = [UserManager sharedUserManager].curUserInfo.username;
    _codeLb.text = [NSString stringWithFormat:@"核销串号：%@", [phone substringFromIndex: phone.length - 4]];
    
    // 刷新活动详请
    [self loadData];
     
    // 支付成功
     HEPayResultResViewController *vc = [HEPayResultResViewController new];
     [self.navigationController qmui_pushViewController:vc animated:YES completion:^{
     }];
}

- (void)payFialure {

}

 
#pragma mark - getters
// scrollView
- (UIScrollView *)scrollView {
    if (_scrollView == nil) {
        _scrollView = [[UIScrollView alloc] init];
        _scrollView.backgroundColor = KWhiteColor;
    }
    return _scrollView;
}

// 约束参照视图
- (UIView *)contentView {
    if (_contentView == nil) {
        _contentView = [[UIView alloc] init];
        _contentView.backgroundColor = KWhiteColor;
    }
    return _contentView;
}

@end
